import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:leader_help/global/user/logic.dart';
import 'package:leader_help/page/tab_controller/controller.dart';
import 'package:leader_help/utils/component/component.dart';

import '../util.dart';
import 'dialog/drop.dart';

class DragableItemView extends StatelessWidget {
  const DragableItemView({
    Key? key,
    required this.itemSize,
    required this.md,
  }) : super(key: key);
  final double itemSize;
  final UsuallyModel md;
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: md.onTap,
      child: Container(
        width: itemSize,
        height: itemSize,
        color: ColorsUtil.bg,
        child: MenuItemView(
            icon: Icon(md.iconData, color: ColorsUtil.red),
            spacing: 0,
            title: Text(
              md.name,
              style: pfMediumStyle(color: ColorsUtil.title, fontSize: 14.sp),
            )),
      ),
    );
  }
}

class DragableListView extends StatelessWidget {
  const DragableListView({Key? key, required this.itemSize, required this.list})
      : super(key: key);
  final double itemSize;
  final List<UsuallyModel> list;
  @override
  Widget build(BuildContext context) {
    return Column(
      children: list
          .map((e) => DragableItemView(
                itemSize: itemSize,
                md: e,
              ))
          .toList(),
    );
  }
}

class SleepDragableView extends StatelessWidget {
  const SleepDragableView({
    Key? key,
    required this.icon,
    required this.activeIcon,
    required this.isExpand,
  }) : super(key: key);
  final IconData icon;
  final IconData activeIcon;
  final bool isExpand;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50.r,
      width: 50.r,
      margin: EdgeInsets.only(right: 10.r),
      decoration: BoxDecoration(
          color: ColorsUtil.red,
          borderRadius: BorderRadius.circular(25.r),
          boxShadow: [
            BoxShadow(
                color: ColorsUtil.red.withAlpha(80),
                offset: const Offset(1, 2)),
            BoxShadow(
                color: ColorsUtil.red.withAlpha(60),
                offset: const Offset(2, 3)),
          ]),
      child: Icon(
        isExpand ? activeIcon : icon,
        size: 25.r,
        color: Colors.white,
      ),
    );
  }
}

void dragableExpandItem({
  required IconData icon,
  required IconData activeIcon,
  required Widget expandWidget,
  required double expandHeight,
}) {
  var _bottom = 180.h.obs;
  var _itemSize = 50.r;
  var _isExpand = false.obs;
  var _isNeedDownExpand = false;
  final mainCtrl = Get.find<TbController>();

  List<Widget> _getExpandWidgets() {
    List<Widget> items = [
      expandWidget,
      SleepDragableView(
        icon: icon,
        activeIcon: activeIcon,
        isExpand: _isExpand.value,
      )
    ];
    if (_isNeedDownExpand) {
      return items.reversed.toList();
    }
    return items;
  }

  var _item = OverlayEntry(
    maintainState: true,
    builder: (ctx) {
      return Obx(
        () => !mainCtrl.state.isShowFloatObs.value
            ? const SizedBox()
            : Positioned(
                right: 0,
                bottom: _isNeedDownExpand
                    ? (_bottom.value - expandHeight)
                    : _bottom.value,
                child: GestureDetector(
                  onTap: () {
                    if (_isExpand.value) {
                      _isNeedDownExpand = false;
                    } else {
                      _isNeedDownExpand = _bottom.value + expandHeight >
                          Get.height - SizeFit.navBarHeight;
                    }

                    _isExpand.toggle();
                  },
                  child: _isExpand.value
                      ? Column(
                          crossAxisAlignment: CrossAxisAlignment.end,
                          children: _getExpandWidgets(),
                        )
                      : Draggable(
                          axis: Axis.vertical,
                          feedback: SleepDragableView(
                            icon: icon,
                            activeIcon: activeIcon,
                            isExpand: _isExpand.value,
                          ),
                          child: SleepDragableView(
                            icon: icon,
                            activeIcon: activeIcon,
                            isExpand: _isExpand.value,
                          ),
                          onDragUpdate: (details) {
                            if (_isExpand.value) {
                              return;
                            }
                            var _maxY = _bottom.value;

                            _maxY -= details.delta.dy;

                            if (_maxY <= Get.bottomBarHeight) {
                              _maxY = Get.bottomBarHeight;
                            }
                            if (_maxY >=
                                Get.height - SizeFit.navBarHeight - _itemSize) {
                              _maxY =
                                  Get.height - SizeFit.navBarHeight - _itemSize;
                            }

                            _bottom.value = _maxY;
                          },
                          onDragEnd: (details) {
                            if (_bottom.value < Get.bottomBarHeight) {
                              _bottom.value = Get.bottomBarHeight;
                            }
                            if (_bottom.value >=
                                Get.height - SizeFit.navBarHeight - _itemSize) {
                              _bottom.value =
                                  Get.height - SizeFit.navBarHeight - _itemSize;
                            }
                          },
                        ),
                ),
              ),
      );
    },
  );

  Overlay.of(Get.context!)?.insert(_item);
}

void dragableItem({
  required VoidCallback onTap,
  required Widget child,
  required double size,
  double? bottom,
}) {
  RxDouble _right = gap12.obs;
  var _bottom = (bottom ?? 120.h).obs;
  var _itemSize = size;
  final mainCtrl = Get.find<TbController>();

  var _item = OverlayEntry(
    maintainState: true,
    builder: (ctx) {
      return Obx(
        () => (!mainCtrl.state.isShowAudioFloatObs.value ||
                !mainCtrl.state.isShowFloatObs.value)
            ? const SizedBox()
            : Positioned(
                right: _right.value,
                bottom: _bottom.value,
                // width: _itemSize,
                // height: _height.value,
                child: GestureDetector(
                  onTap: onTap,
                  child: Draggable(
                    feedback: child,
                    child: child,
                    onDragUpdate: (details) {
                      var _maxX = _right.value;
                      var _maxY = _bottom.value;

                      _maxX -= details.delta.dx;
                      _maxY -= details.delta.dy;
                      if (_maxX < gap12) {
                        _maxX = gap12;
                      }
                      if (_maxX > Get.width - gap12 - _itemSize) {
                        _maxX = Get.width - gap12 - _itemSize;
                      }
                      if (_maxY <= Get.bottomBarHeight) {
                        _maxY = Get.bottomBarHeight;
                      }
                      if (_maxY >=
                          Get.height - SizeFit.navBarHeight - _itemSize) {
                        _maxY = Get.height - SizeFit.navBarHeight - _itemSize;
                      }
                      _right.value = _maxX;
                      _bottom.value = _maxY;
                    },
                    onDragEnd: (details) {
                      if (_right.value < gap12) {
                        _right.value = gap12;
                      }
                      if (_right.value < (Get.width - _itemSize) / 2) {
                        _right.value = gap12;
                      } else {
                        _right.value = Get.width - gap12 - _itemSize;
                      }
                      if (_bottom.value < Get.bottomBarHeight) {
                        _bottom.value = Get.bottomBarHeight;
                      }
                      if (_bottom.value >=
                          Get.height - SizeFit.navBarHeight - _itemSize) {
                        _bottom.value =
                            Get.height - SizeFit.navBarHeight - _itemSize;
                      }
                    },
                  ),
                ),
              ),
      );
    },
  );
  Overlay.of(Get.context!)?.insert(_item);
}
